#!/bin/bash
# macOS Build Script for Ollama Translator

echo "🚀 Building Ollama Translator for macOS..."

# Check if running on macOS
if [[ "$OSTYPE" != "darwin"* ]]; then
    echo "❌ This script must be run on macOS"
    exit 1
fi

# Check dependencies
if ! command -v python3 &> /dev/null; then
    echo "❌ Python 3 not found. Please install Python 3.8+"
    exit 1
fi

# Create build directories
mkdir -p build_macos dist_macos

# Install dependencies if needed
echo "📦 Installing dependencies..."
python3 -m pip install --upgrade pip
python3 -m pip install requests PyQt6 PyInstaller

# Build using PyInstaller with macOS-specific settings
echo "🔨 Building executable..."
python3 -m PyInstaller \
    --onefile \
    --noconsole \
    --name "ollama_translate" \
    --workpath build_macos \
    --distpath dist_macos \
    --osx-bundle-identifier "com.ollama.translator" \
    --add-data "README.md:." \
    --hidden-import config_manager \
    --hidden-import ollama_client \
    --hidden-import translator_gui \
    --hidden-import config_window \
    --hidden-import settings_dialog \
    main.py

# Create macOS app bundle
echo "📱 Creating macOS app bundle..."
APP_NAME="Ollama Translator.app"
APP_PATH="dist_macos/$APP_NAME"

# Remove existing app bundle
rm -rf "$APP_PATH"

# Create app bundle structure
mkdir -p "$APP_PATH/Contents/MacOS"
mkdir -p "$APP_PATH/Contents/Resources"

# Copy executable
cp "dist_macos/ollama_translate" "$APP_PATH/Contents/MacOS/Ollama Translator"

# Create Info.plist
cat > "$APP_PATH/Contents/Info.plist" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>en</string>
    <key>CFBundleExecutable</key>
    <string>Ollama Translator</string>
    <key>CFBundleIdentifier</key>
    <string>com.ollama.translator</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>Ollama Translator</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0.0</string>
    <key>CFBundleVersion</key>
    <string>1</string>
    <key>LSMinimumSystemVersion</key>
    <string>10.13</string>
    <key>NSHighResolutionCapable</key>
    <true/>
</dict>
</plist>
EOF

# Make executable executable
chmod +x "$APP_PATH/Contents/MacOS/Ollama Translator"

# Create DMG installer script
cat > "dist_macos/create_dmg.sh" << 'EOF'
#!/bin/bash
# Create DMG installer for macOS

APP_NAME="Ollama Translator.app"
DMG_NAME="Ollama_Translator.dmg"
VOLUME_NAME="Ollama Translator"

# Create temporary directory
TEMP_DIR="temp_dmg"
rm -rf "$TEMP_DIR"
mkdir -p "$TEMP_DIR"

# Copy app to temp directory
cp -r "$APP_NAME" "$TEMP_DIR/"

# Create Applications alias
ln -s "/Applications" "$TEMP_DIR/Applications"

# Create background (optional)
# mkdir -p "$TEMP_DIR/.background"

# Create DMG
hdiutil create \
    -volname "$VOLUME_NAME" \
    -srcfolder "$TEMP_DIR" \
    -ov \
    -format UDZO \
    "$DMG_NAME"

# Clean up
rm -rf "$TEMP_DIR"

echo "DMG created: $DMG_NAME"
EOF

chmod +x "dist_macos/create_dmg.sh"

# Create installation script
cat > "dist_macos/install_macos.sh" << 'EOF'
#!/bin/bash
# macOS Installation Script

echo "Installing Ollama Translator..."

# Check if Ollama is running
if ! pgrep -xq "ollama"; then
    echo "⚠️  Warning: Ollama service is not running"
    echo "Please start Ollama with: brew services start ollama"
fi

# Copy to Applications folder
if [ -d "/Applications" ]; then
    cp -r "Ollama Translator.app" "/Applications/"
    echo "✅ Installed to /Applications/Ollama Translator.app"
    echo ""
    echo "To run the application:"
    echo "  Open Finder, go to Applications, and double-click 'Ollama Translator'"
else
    echo "❌ Cannot access /Applications folder"
    echo "You can run the app directly from this folder"
fi

echo ""
echo "Installation complete!"
EOF

chmod +x "dist_macos/install_macos.sh"

# Verify the build
if [ -f "dist_macos/ollama_translate" ]; then
    SIZE=$(du -h "dist_macos/ollama_translate" | cut -f1)
    echo "✅ Build successful!"
    echo "📁 Executable: dist_macos/ollama_translate ($SIZE)"
    echo "📱 App bundle: dist_macos/Ollama Translator.app"
    echo ""
    echo "To run the application:"
    echo "  ./dist_macos/ollama_translate"
    echo ""
    echo "To install:"
    echo "  cd dist_macos && ./install_macos.sh"
else
    echo "❌ Build failed"
    exit 1
fi